﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SQLServerHealthCheck.SQLServer;
namespace SQLServerHealthCheck.PL
{
    public partial class TraceLookup : Form
    {
       

        public TraceLookup()
        {
            InitializeComponent();
        }

        private void TraceLookup_Load(object sender, EventArgs e)
        {
            LoadGridView();
        }

        private void LoadGridView()
        {
            //selected SQLInstance in HealthCheckControl Form

            if (InstallationCheck.IsSQLServerAccessible(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringTemplate"].ToString()))
            {
                              
           
                string strselectedSQLInstance = HealthCheckControlPanel.strselectedSQLInstance.ToString();
         
                SqlConnection cnHealthCheck = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringTemplate"]);
                cnHealthCheck.Open();

                DataTable dt = new DataTable();

                SqlCommand ocmd=new SqlCommand();
                ocmd.CommandType = CommandType.Text;
                ocmd.CommandText = "select ServerTraceId,ServerName,DatabaseName,TraceStartDate,TraceEndDate,TraceStartTime,TraceEndTime from Trace_Server where ServerName='" + strselectedSQLInstance + "' order by ServerTraceId desc ";
                ocmd.Connection = cnHealthCheck;

                SqlDataAdapter sqldadap = new SqlDataAdapter(ocmd);
                sqldadap.Fill(dt);

                dGViewTraceDetas.DataSource = dt;

           
                dGViewTraceDetas.Columns[1].Visible = false;
                dGViewTraceDetas.Columns[2].Visible = false;

                for(int i=1; i<dGViewTraceDetas.Columns.Count;i++)
                {
                    dGViewTraceDetas.Columns[i].ReadOnly = true;
                }


            }
            else
            {
                MessageBox.Show("The SQLServerHealthCheck database to store health check details is inaccessible.Please check if database server is up or check the config file to see a valid Server name has been entered.", "SQLServerHealthCheck", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void cmdImportTrace_Click(object sender, EventArgs e)
        {

            string selectedIndex = "";
         
            foreach (DataGridViewRow dr in dGViewTraceDetas.Rows)
            {

                if (dr.Cells[0].Value != null)
                {
                    if (selectedIndex.Trim().Length == 0)
                    {
                        selectedIndex =  dr.Cells[1].Value.ToString();
                    }
                    else
                    {
                        selectedIndex = selectedIndex + "," + dr.Cells[1].Value.ToString();
                    }
                }
                   
            }

            if (selectedIndex.Trim().Length == 0)
            {
                MessageBox.Show("Please select a trace file to import", "SQLServerHealthCheck", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
                 
                //import the trace files
            else
            {
                SQLTrace.ImportTraceDetailsfromServer(selectedIndex.Split(','));
                this.Close();
               
            }
         


        }

       

    }
}
